import React, { useEffect, useState } from 'react'

export default function useIsPageTop(target) {
  const [isTop, setIsTop] = useState(false)

  function onScroll() {
    if (target) {
      let dom = target.current;
      const scrTop = document.documentElement.scrollTop || window.pageYOfset || document.body.scrollTop;
      // console.log(top, 'scrollTop')
      if (dom) {
        let domTop = dom.offsetTop;
        let scrollTop = scrTop;
        if (scrollTop >= domTop) {
          setIsTop(true)
        } else {
          setIsTop(false)
        }
      }

    }
  }

  useEffect(() => {
    document.addEventListener('scroll', onScroll)
    // return () => {
    //   target.current.removeEventListener('scroll')
    // }
  }, [])

  return isTop
}

